package cn.edu.zust.se.springmvctest.controller;

import cn.edu.zust.se.springmvctest.model.R;
import cn.edu.zust.se.springmvctest.model.User;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.nio.charset.Charset;

/**
 * 
 * @author czg
 * @since 2022/11/23 15:02
 */
@Controller
@RequestMapping("user")
public class UserController {

    @RequestMapping(value = "login",method = RequestMethod.GET)
    public String login(HttpServletRequest request, HttpServletResponse response){
        System.out.println("进入登录页面");
        return "hello";
    }
    @RequestMapping(value = "login",method = RequestMethod.POST)
    public String doLogin(@RequestParam(value = "userName",required=false) String name, String password){
        System.out.println("用户登录：userName:"+name+",password:"+password);
        return "hello";
    }
    @RequestMapping("add")
    public String addUser(String userName,String name,String password,int age){
        System.out.println("UserAdd:userName="+userName+
                ",name="+name+",password="+password+",age="+age);
        return "user/add";
    }
    @ResponseBody
    @RequestMapping("addObj")
//    ?name=zhangsn&userName=aaa&password=123456&age=20
    //绑定给user对象的对应属性
    public User addUserObj(@RequestBody User user){
        System.out.println("UserAdd:userName="+user.getUserName()+
                ",name="+user.getName()+",password="+user.getPassword()+",age="+user.getAge());
        return user;
    }
//    @RequestMapping("login")
//    public ModelAndView login(String userName, String password){
//        ModelAndView mav=new ModelAndView();
//        mav.setViewName("user/login");
//        mav.addObject("userName",userName);
//        System.out.println("User Login:userName="+userName+",password="+password);
//        return mav;
//    }
    @RequestMapping("add-user")
    @ResponseBody
    public R<User> add(@RequestBody @Valid User user, BindingResult br){
        if(br.hasErrors()){
            return R.fail("数据校验失败");
        }
        return R.ok(user,"请求成功");
    }
    @RequestMapping("testEntity")
    public ResponseEntity<User> testUser(HttpEntity<User> entity){
        User user=entity.getBody();
        System.out.println(JSON.toJSONString(user));
        HttpHeaders headers = new HttpHeaders();
        MediaType mt1=new MediaType("application","json");
        headers.setContentType(mt1);
        ResponseEntity<User> resp=new ResponseEntity<User>(user,headers,HttpStatus.OK);
        return resp;
    }
    @RequestMapping("test")
    public String test(String userName, String password, HttpServletRequest request){
        User u=new User();
        u.setName(userName);
        u.setPassword(password);
        request.setAttribute("user",u);
        return "user/user";
    }
    @RequestMapping("test2")
    public String test2(String userName,String password,Model model){
        User u=new User();
        u.setName(userName);
        u.setPassword(password);
        model.addAttribute("user",u);
        return "user/user";
    }
    @RequestMapping("test3")
    public String test3(String userName, String password, ModelMap map){
        User u=new User();
        u.setName(userName);
        u.setPassword(password);
        map.addAttribute("user",u);
        return "user/user";
    }
    @RequestMapping("test4")
    public ModelAndView test4( String userName,String password){
        ModelAndView mav=new ModelAndView();
        User u=new User();
        u.setName(userName);
        u.setPassword(password);
        mav.addObject("user",u);
        mav.setViewName("user/user");
        return mav;
    }
}
